// 这个文件补全数据结构

import Vue from 'vue'
export function completionFunc(fundInfo, title) {
	const data = JSON.parse(JSON.stringify(fundInfo))
	if (!data.productFundApply || ['成立', '查看'].includes(title)) {
		data.productFundApply = {}
		data.dataStatus = '1'// 当没有申请表数据对象时
	} else {
		data.dataStatus = '0'// 当有申请表数据对象时
	}

	/* 补全fundCustodianInfo*/
	if (!data.fundCustodianInfo) {
		data.fundCustodianInfo = {}
	}
	if (!data.fundCustodianInfo.checkLiableId) {
		data.fundCustodianInfo.checkLiableId = ''
	}
	if (!data.fundCustodianInfo.checkLiableName) {
		data.fundCustodianInfo.checkLiableName = ''
	}
	if (!data.fundCustodianInfo.fundSysCode) {
		data.fundCustodianInfo.fundSysCode = ''
	}
	if (!data.fundCustodianInfo.personLiableId) {
		data.fundCustodianInfo.personLiableId = ''
	}
	if (!data.fundCustodianInfo.personLiableName) {
		data.fundCustodianInfo.personLiableName = ''
	}

	if (!data.recordFundAddInfo) {
		data.recordFundAddInfo = {}
	} else {
		if (Object.prototype.toString.call(data.recordFundAddInfo).substring(8, Object.prototype.toString.call(data.recordFundAddInfo).length - 1) !== 'Object') {
			data.recordFundAddInfo = {}
		}
	}

	/* 补全基金经理*/
	if (!data.fundManagerApplyList) {
		data.fundManagerApplyList = JSON.parse(JSON.stringify([]))
	}
	if (!data.fundManagerInfoList) {
		data.fundManagerApplyList = JSON.parse(JSON.stringify([]))
	}

	/* 补全recordFundChangeInfo*/
	if (!data.recordFundChangeInfo) {
		data.recordFundChangeInfo = {}
	}

	/* 补全主表和申请表*/
	if (!data.productFundInfo) {
		data.productFundInfo = {}
	}

	/* 补全基础信息*/
	const baseInfoKeys = [
		{ key: 'personLiableName', type: 'str' },
		{ key: 'riskGrade', type: 'str' },
		{ key: 'isInnovateNature', type: 'str' },
		{ key: 'innovateNatureRemark', type: 'str' },
		{ key: 'fundCode', type: 'str' },
		{ key: 'fundOutsideCode', type: 'str' },
		{ key: 'fundFullName', type: 'str' },
		{ key: 'fundName', type: 'str' },
		{ key: 'fundEnglishName', type: 'str' },
		{ key: 'evolveBeforeCode', type: 'str' },
		{ key: 'performanceBaseline', type: 'str' },
		{ key: 'fiduciaryDuty', type: 'str' },
		{ key: 'expectRaiseAmount', type: 'str' },
		{ key: 'settleCurrency', type: 'str' },
		{ key: 'faceValue', type: 'str' },
		{ key: 'operateMode', type: 'str' },
		{ key: 'openFrequency', type: 'str' },
		{ key: 'openFrequencyRemark', type: 'str' },
		{ key: 'incomeDisWay', type: 'str' },
		{ key: 'isHolderMeet', type: 'str' },
		{ key: 'fundType', type: 'str' },
		{ key: 'planType', type: 'str' },
		{ key: 'oneYardPassAccount', type: 'str' },
		{ key: 'productBrand', type: 'str' },
		{ key: 'productPeriod', type: 'str' },
		{ key: 'managerStyle', type: 'str' },
		{ key: 'issuerEarlyTermFlag', type: 'str' },
		{ key: 'usufructTransferProductFlag', type: 'str' },
		{ key: 'currencyMarketFundFlag', type: 'str' },
		{ key: 'isChannelBusiness', type: 'str' },
		{ key: 'isQdii', type: 'str' },
		{ key: 'isIndexFund', type: 'str' },
		{ key: 'isMom', type: 'str' },
		{ key: 'isSubAssets', type: 'str' },
		{ key: 'momFundCode', type: 'str' },
		{ key: 'isPrivateFund', type: 'str' },
		{ key: 'privateFundType', type: 'str' },
		{ key: 'isPensionProduct', type: 'str' },
		{ key: 'isInstAcc', type: 'str' },
		{ key: 'isBasicPension', type: 'str' },
		{ key: 'isSocialSecurity', type: 'str' },
		{ key: 'isOccupationalPension', type: 'str' },
		{ key: 'isAnnuityPension', type: 'str' },
		{ key: 'annuityPlanType', type: 'str' },
		{ key: 'annuityTrusteeMode', type: 'str' },
		{ key: 'isAddcreditMeasure', type: 'str' },
		{ key: 'addcreditMeasureType', type: 'str' },
		{ key: 'isNetWorthProduct', type: 'str' },
		{ key: 'valuationWay', type: 'str' },
		{ key: 'valuationFrequency', type: 'str' },
		{ key: 'isStructuredProduct', type: 'str' },
		{ key: 'gradingRatio', type: 'str' },
		{ key: 'isFofFund', type: 'str' },
		{ key: 'establishScale', type: 'str' },
		{ key: 'remark', type: 'str' },
		{ key: 'productAssetType', type: 'str' },
		{ key: 'productSpecialType', type: 'str' },
		{ key: 'productAssetReportType', type: 'str' },
		{ key: 'isSpecialAssetReport', type: 'str' },
		{ key: 'isAssetSecuritizationBusiness', type: 'str' },
		{ key: 'customerRedemptionFlag', type: 'str' },
		{ key: 'isAssetManagementProduct', type: 'str' },
		{ key: 'associationOpenFrequency', type: 'str' },
		{ key: 'crossBorderFinancial', type: 'str' },
		{ key: 'propertyFundTrust', type: 'str' },
		{ key: 'noneHKTIsStopApplyRansom', type: 'str' }
	]

	if (!data.productFundInfo.baseInfo) {
		data.productFundInfo.baseInfo = {}
	}
	if (!data.productFundApply.baseInfo) {
		data.productFundApply.baseInfo = {}
	}
	if (!data.productFundInfo.baseInfo.branchShareList || !Array.isArray(data.productFundInfo.baseInfo.branchShareList)) {
		data.productFundInfo.baseInfo.branchShareList = []
	}
	if (!data.productFundApply.baseInfo.branchShareList || !Array.isArray(data.productFundApply.baseInfo.branchShareList)) {
		data.productFundApply.baseInfo.branchShareList = []
	}
	baseInfoKeys.forEach(item => {
		if (!data.productFundInfo.baseInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.baseInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.baseInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
		if (!data.productFundApply.baseInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.baseInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.baseInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
	})

	/* 补全重要日期信息*/
	const dateInfoKeys = [
		{ key: 'finishFilingDate', type: 'str' },
		{ key: 'initialFilingDate', type: 'str' },
		{ key: 'raiseStartDate', type: 'str' },
		{ key: 'establishDate', type: 'str' },
		{ key: 'contractEndDate', type: 'str' },
		{ key: 'contractEndDatePeople', type: 'str' },
		{ key: 'liquidateAccompleteDate', type: 'str' },
		{ key: 'operateEndDate', type: 'str' },
		{ key: 'liquidationStartDate', type: 'str' },
		{ key: 'subsistTerm', type: 'str' },
		{ key: 'formalEvolveDate', type: 'str' },
		{ key: 'nextOpenFirstDate', type: 'str' },
		{ key: 'nextOpenFirstDate', type: 'str' }
	]
	if (!data.productFundInfo.dateInfo) {
		data.productFundInfo.dateInfo = {}
	}
	if (!data.productFundApply.dateInfo) {
		data.productFundApply.dateInfo = {}
	}
	dateInfoKeys.forEach(item => {
		if (!data.productFundInfo.dateInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.dateInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.dateInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
		if (!data.productFundApply.dateInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.dateInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.dateInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
	})

	/* 补全机构信息*/
	const organInfoKeys = [
		{ key: 'managementId', type: 'str' },
		{ key: 'fundTaType', type: 'str' },
		{ key: 'trusteeId', type: 'str' },
		{ key: 'custodyBranchOrganName', type: 'str' },
		{ key: 'isThirdPartyCustody', type: 'str' },
		{ key: 'investAdvisorId', type: 'str' },
		{ key: 'lawId', type: 'str' },
		{ key: 'handleLawerName', type: 'str' },
		{ key: 'accountFirmId', type: 'str' },
		{ key: 'bordersCustodianName', type: 'str' },
		{ key: 'bordersCustodianEnglishName', type: 'str' },
		{ key: 'bordersAdvisorName', type: 'str' },
		{ key: 'bordersAdvisorEnglishName', type: 'str' },
		{ key: 'investAdvisorOrgan', type: 'str' },
		{ key: 'investAdvisorRate', type: 'str' },
		{ key: 'iscustodyBranchOrgan', type: 'str' }
	]
	if (!data.productFundInfo.organInfo) {
		data.productFundInfo.organInfo = {}
	}
	if (!data.productFundApply.organInfo) {
		data.productFundApply.organInfo = {}
	}
	organInfoKeys.forEach(item => {
		if (!data.productFundInfo.organInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.organInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.organInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
		if (!data.productFundApply.organInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.organInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.organInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
	})

	/* 补全费率信息*/
	const rateInfoKeys = [
		{ key: 'feeRateAlgorithm', type: 'str' },
		{ key: 'isAchieveReward', type: 'str' },
		{ key: 'accrualPointTime', type: 'str' },
		{ key: 'rewardExplain', type: 'str' },
		{ key: 'accrualDatum', type: 'str' },
		{ key: 'accrualRatio', type: 'str' },
		{ key: 'accrualRatio', type: 'str' },
		{ key: 'custodyFeeAlgorithm', type: 'str' },
		{ key: 'feeRateInfoList', type: 'arr' }
	]
	if (!data.productFundInfo.rateInfo) {
		data.productFundInfo.rateInfo = {}
	}
	if (!data.productFundApply.rateInfo) {
		data.productFundApply.rateInfo = {}
	}
	rateInfoKeys.forEach(item => {
		if (!data.productFundInfo.rateInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.rateInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.rateInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
		if (!data.productFundApply.rateInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.rateInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.rateInfo[item.key] = JSON.parse(JSON.stringify([]))
			}
		}
	})
	const listapply = rateListFunc(
		data.productFundApply.rateInfo.feeRateInfoList
	)
	data.productFundApply.rateInfo.feeRateInfoList = JSON.parse(JSON.stringify(listapply))
	const listinfo = rateListFunc(
		data.productFundInfo.rateInfo.feeRateInfoList
	)
	data.productFundInfo.rateInfo.feeRateInfoList = JSON.parse(JSON.stringify(listinfo))

	/* 补全投资相关信息*/
	const investInfoKeys = [
		{ key: 'specialTypeChar', type: 'str' },
		{ key: 'isContainSpclLimit', type: 'str' },
		{ key: 'specialTypeName', type: 'str' },
		{ key: 'spclLimitRemark', type: 'str' },
		{ key: 'isCoverTactics', type: 'str' },
		{ key: 'stockMinRatio', type: 'str' },
		{ key: 'stockMaxRatio', type: 'str' },
		{ key: 'bondMinRatio', type: 'str' },
		{ key: 'bondMaxRatio', type: 'str' },
		{ key: 'currencyMinRatio', type: 'str' },
		{ key: 'currencyMaxRatio', type: 'str' },
		{ key: 'hkStockMaxRatio', type: 'str' },
		{ key: 'remark', type: 'str' }
	]
	if (!data.productFundInfo.investInfo) {
		data.productFundInfo.investInfo = {}
	}
	if (!data.productFundApply.investInfo) {
		data.productFundApply.investInfo = {}
	}
	investInfoKeys.forEach(item => {
		if (!data.productFundInfo.investInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.investInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.investInfo[item.key] = []
			}
		}
		if (!data.productFundApply.investInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.investInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.investInfo[item.key] = []
			}
		}
	})

	/* 补全文件信息*/
	if (!data.productFundInfo.fundFileInfo) {
		data.productFundInfo.fundFileInfo = {}
	}
	if (!data.productFundApply.fundFileInfo) {
		data.productFundApply.fundFileInfo = {}
	}

	/* 补全风控信息*/
	const riskInfoKeys = [
		{ key: 'isRiskMeasures', type: 'str' },
		{ key: 'investLeverRate', type: 'str' },
		{ key: 'earlyWarnLine', type: 'str' },
		{ key: 'earlyWarnNetWorth', type: 'str' },
		{ key: 'stopLossLine', type: 'str' },
		{ key: 'stopLossNetWorth', type: 'str' },
		{ key: 'maxRetreat', type: 'str' },
		{ key: 'otherRiskStep', type: 'str' },
		{ key: 'otherRiskExpression', type: 'str' },
		{ key: 'managerIsInvestment', type: 'str' },
		{ key: 'managerInvestmentAmount', type: 'str' },
		{ key: 'managerInvestmentRatio', type: 'str' },
		{ key: 'managerPartakeAmount', type: 'str' },
		{ key: 'otherRiskStep', type: 'str' },
		{ key: 'managerIsPartake', type: 'str' },
		{ key: 'managerPartakeRatio', type: 'str' },
		// 2022.1.4
		{ key: 'earlyWarnLineType', type: 'str' },
		{ key: 'stopLossLineType', type: 'str' },
		{ key: 'retreatWarnLineType', type: 'str' },
		{ key: 'retreatEarlyWarnNetWorth', type: 'str' },
		{ key: 'retreatStopLossLineType', type: 'str' },
		{ key: 'retreatStopLossNetWorth', type: 'str' },
		{ key: 'stopLossWarningLine', type: 'str' }
	]
	if (!data.productFundInfo.riskInfo) {
		data.productFundInfo.riskInfo = {}
	}
	if (!data.productFundApply.riskInfo) {
		data.productFundApply.riskInfo = {}
	}
	riskInfoKeys.forEach(item => {
		if (!data.productFundInfo.riskInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.riskInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.riskInfo[item.key] = []
			}
		}
		if (!data.productFundApply.riskInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.riskInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.riskInfo[item.key] = []
			}
		}
	})

	/* 补全委托人信息 */
	const consignerInfoKeys = [
		{ key: 'consignerName', type: 'str' },
		{ key: 'subjectType', type: 'str' },
		{ key: 'csrcType', type: 'str' },
		{ key: 'certificateType', type: 'str' },
		{ key: 'certificateNumber', type: 'str' },
		{ key: 'peopleMax', type: 'str' },
		{ key: 'peopleMin', type: 'str' },
		{ key: 'city', type: 'str' },
		{ key: 'province', type: 'str' }
	]
	if (!data.productFundInfo.consignerInfo) {
		data.productFundInfo.consignerInfo = {}
	}
	if (!data.productFundApply.consignerInfo) {
		data.productFundApply.consignerInfo = {}
	}
	consignerInfoKeys.forEach(item => {
		if (!data.productFundInfo.consignerInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundInfo.consignerInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundInfo.consignerInfo[item.key] = []
			}
		}
		if (!data.productFundApply.consignerInfo[item.key]) {
			if (item.type === 'str') {
				data.productFundApply.consignerInfo[item.key] = ''
			} else if (item.type === 'arr') {
				data.productFundApply.consignerInfo[item.key] = []
			}
		}
	})

	return data
}

// 如果哪天你维护到这段代码，建议轻易不要动
// 补全费率数组
/*
rate 费率列表
*/
function rateListFunc(rate) {
	const feeRateInfoList = jsonArray(rate)
	let rateList = []
	const manageRate = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '9'), '9', 1) // 处理管理费率
	const trusteeRate = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '7'), '7', 1) // 处理托管费率
	const salesSerivceRateList = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '8'), '8', 1) // 处理销售服务费率
	const branchSubRateListList = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '1'), '1', 4) // 处理普通认购费率
	const branchApplyRateListList = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '3'), '3', 4) // 处理前端-普通申购费率
	const rearRndRateList = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '10'), '10', 4) // 处理后端-普通申购费率
	const branchRansomRateListList = spliceIsPush(feeRateInfoList.filter(item => item.feeRateType === '5'), '5', 6) // 处理普通赎回费率
	rateList = [
		...jsonArray(rateList),
		...jsonArray(manageRate),
		...jsonArray(trusteeRate),
		...jsonArray(salesSerivceRateList),
		...jsonArray(branchSubRateListList),
		...jsonArray(branchApplyRateListList),
		...jsonArray(rearRndRateList),
		...jsonArray(branchRansomRateListList)
	]
	rateList.forEach(item => {
		const keys = Object.keys(item)
		keys.forEach(items => {
			if (!item[items]) {
				item[items] = ''
			}
		})
		if (item.feeRateType === '5') {
			item.belongFundRatio = item.belongFundRatio ? item.belongFundRatio : ''
			item.belongFundRatioUnit = item.belongFundRatioUnit ? item.belongFundRatioUnit : '1'
			item.remark = item.remark ? item.remark : ''
		}
		if (item.feeRateType !== '5') {
			if ('belongFundRatio' in item) {
				Vue.delete(item, 'belongFundRatio')
			}
			if ('belongFundRatioUnit' in item) {
				Vue.delete(item, 'belongFundRatioUnit')
			}
			item.remark = item.remark ? item.remark : ''
		}
		if (['1', '2', '3', '4'].includes(item.feeRateType)) {
			item.itemSign = 'M'
		}
		if (['5', '6', '10', '11'].includes(item.feeRateType)) {
			item.itemSign = 'N'
		}
		item.subFundType = item.subFundType ? item.subFundType : ''
		item.fundCode = item.fundCode ? item.fundCode : ''
	})
	return jsonArray(rateList)
}

// 处理费率数组
// rate 要处理的数组
// rateType 费率类型
// type 数组长度类型
function spliceIsPush(rate, rateType, type) {
	const list = [...JSON.parse(JSON.stringify(rate))]
	let rateArray = []
	if (type === 4) { // 长度为4的类型的数组
		if (list.length > 4) {
			rateArray = JSON.parse(JSON.stringify(list.filter((item, index) => index < 4)))
		} else if (list.length < 4) {
			const addLength = list.length
			for (let i = addLength; i < 4; i++) {
				let j = 1; j += i
				const obj = { feeRateType: rateType, feeRateId: `${j}`, startSection: '', endSection: '', feeRate: '', feeRateUnit: '1' }
				list.push(JSON.parse(JSON.stringify(obj)))
			}
			rateArray = JSON.parse(JSON.stringify(list))
		} else if (list.length === 4) {
			rateArray = JSON.parse(JSON.stringify(list))
		}
	} else if (type === 6) { // 长度为6的类型的数组
		if (list.length > 6) {
			rateArray = JSON.parse(JSON.stringify(list.filter((item, index) => index < 6)))
		} else if (list.length < 6) {
			const addLength = list.length
			for (let i = addLength; i < 6; i++) {
				let j = 1; j += i
				const obj = { feeRateType: rateType, feeRateId: `${j}`, startSection: '', endSection: '', feeRate: '', feeRateUnit: '1', belongFundRatio: '', belongFundRatioUnit: '1' }
				list.push(JSON.parse(JSON.stringify(obj)))
			}
			rateArray = JSON.parse(JSON.stringify(list))
		} else if (list.length === 6) {
			rateArray = JSON.parse(JSON.stringify(list))
		}
	} else if (type === 1) { // 长度为6的类型的数组
		if (list.length > 1) {
			rateArray = JSON.parse(JSON.stringify(list.filter((item, index) => index < 1)))
		} else if (list.length < 1) {
			const addLength = list.length
			for (let i = addLength; i < 1; i++) {
				let j = 1; j += i
				const obj = { feeRateType: rateType, feeRateId: `${j}`, startSection: '', endSection: '', feeRate: '', feeRateUnit: '1' }
				list.push(JSON.parse(JSON.stringify(obj)))
			}
			rateArray = JSON.parse(JSON.stringify(list))
		} else if (list.length === 1) {
			rateArray = JSON.parse(JSON.stringify(list))
		}
	}

	return JSON.parse(JSON.stringify(rateArray))
}
function jsonArray(list) { // 将数组序列化
	return JSON.parse(JSON.stringify(list))
}
